<template>
  <el-row type="flex" justify="space-between" align="middle" style="width:100%;height:50px;">
    <el-col>
      <span>{{ treeTools.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="space-between" align="middle">
        <el-col>{{ treeTools.manager }}</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span>
              操作 <i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeTools: {
      type: Object,
      require: true,
      default: () => {
        return {}
      }
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async  operateDepts(command) {
      switch (command) {
        case 'add':
          this.$emit('addDept', this.treeTools)
          break
        case 'edit':
          this.$emit('addDept', this.treeTools, command)

          break
        case 'del':
          try {
            await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
            await delDepartments(this.treeTools.id)
            this.$emit('delDepts')
            this.$message.success('删除了')
          } catch (error) {
            this.$message.error('取消删除')
          }
          break
      }
    }
  }
}
</script>

<style>

</style>
